package com.fudan.backend.controller;

import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.Store;
import com.fudan.backend.pojo.vo.StoreVo;
import com.fudan.backend.service.StoreService;
import com.fudan.backend.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/store")
public class StoreController {

    @Autowired
    StoreService storeService;

    @GetMapping("/info/{storeId}")
    public R getStoreByStoreId(@PathVariable("storeId") String storeId){
        Store store = storeService.getStoreByStoreId(storeId);
        return new R(RetCode.STORE_SUCCESS.getCode(),RetCode.STORE_SUCCESS.getMessage(),RetCode.STORE_SUCCESS.getMessage(),store);
    }

    @GetMapping("/info")
    public R getAllStore(int current, int size){
        PageInfo<Store> page = storeService.getAllStore(current, size);
        HashMap<String, Object> map = new HashMap<>();
        map.put("storeList", page.getList());
        map.put("total", page.getTotal());
        return new R(RetCode.STORE_SUCCESS.getCode(),RetCode.STORE_SUCCESS.getMessage(),RetCode.STORE_SUCCESS.getMessage(),map);
    }

    @GetMapping("/vo/{storeId}")
    public R getStoreVoByStoreId(@PathVariable("storeId") String storeId){
        StoreVo storeVo = storeService.getStoreVoByStoreId(storeId);
        return new R(RetCode.STORE_SUCCESS.getCode(),RetCode.STORE_SUCCESS.getMessage(),RetCode.STORE_SUCCESS.getMessage(), storeVo);
    }

    @GetMapping("/vo")
    public R getAllStoreVo(int current, int size){
        PageInfo<StoreVo> page = storeService.getAllStoreVo(current, size);
        HashMap<String, Object> map = new HashMap<>();
        map.put("storeVoList", page.getList());
        map.put("total", page.getTotal());
        return new R(RetCode.STORE_SUCCESS.getCode(),RetCode.STORE_SUCCESS.getMessage(),RetCode.STORE_SUCCESS.getMessage(),map);
    }

    @GetMapping("/user/{userId}")
    public R getAllStoreByUserId(@PathVariable("userId") String userId){
        List<Store> stores = storeService.getAllStoreByUserId(userId);
        return new R(RetCode.STORE_SUCCESS.getCode(), RetCode.STORE_SUCCESS.getMessage(), RetCode.STORE_SUCCESS.getMessage(),stores);
    }
}
